package com.ky.pro.big.data.mr.mobile.dr;

import com.ky.pro.big.data.mr.mobile.dr.dto.MobileDataRateInfo;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;

public class MobileDataRateMapper extends Mapper<LongWritable, Text, LongWritable, MobileDataRateInfo> {

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        String line = value.toString();
        String[] split = line.split("\\t");
        // 手机号
        long msisdn;
        int upRate;
        int downRate;
        try {
            // 流量
            msisdn = Long.valueOf(split[1]);
            upRate = Integer.valueOf(split[8]);
            downRate = Integer.valueOf(split[9]);
        } catch (NumberFormatException nfe) {
            System.err.println("数据格式错误：" + split[1] + "," + split[8] + "," + split[9]);
            return;
        } catch (ArrayIndexOutOfBoundsException obe) {
            System.err.println("数据越界异常：" + line);
            return;
        }
        LongWritable msisdnLong = new LongWritable(msisdn);
        MobileDataRateInfo mobileDataRateInfo = new MobileDataRateInfo(msisdn, upRate, downRate);
        context.write(msisdnLong, mobileDataRateInfo);
    }
}
